//
//  ZXYXYAxisView.m
//  Targeting
//
//  Created by zhouxiaoyu on 14-6-24.
//  Copyright (c) 2014年 duostec. All rights reserved.
//

#define maxX @"maxX"
#define minX @"minX"
#define maxY @"maxY"
#define minY @"minY"
#import "ZXYXYAxisView.h"

typedef enum
{
    DataNone = -1,
    DataNUM = 0,
    DataNUMString = 1,
    DataStringNUM = 2,
    DataSringString = 3,
}DataSourceType;

typedef enum
{
    DigitsTypeOne = 1,
    DigitsTypeTen = 10,
    DigitsTypeHund = 100,
    DigitsTypeThoud = 1000,
    DigitsTypeTenThoud = 10000,
}DigitsType;

typedef enum
{
   x_Type = 1,
   y_Type = 2,
   width_Type = 3,
} XYType;
@interface ZXYXYAxisView()
{
    NSInteger numOfData;
    NSMutableArray *allArray;
    NSMutableDictionary *maxOrMinValues;
    id<ZXYAxisDatasource>_dataSource;
    id<ZXYAxisDelegate>_delegate;
    float x_0; /** < 第一个x坐标的位置(view)*/
    float y_0; /** < 第一个y坐标的位置(view)*/
    float x_last;/** < 最后一个x坐标的位置(view)*/
    float y_last; /** < 最后一个y坐标的位置(view)*/
    float x_V0;   /** < 第一个x轴的大小(value)*/
    float y_V0;   /** < 第二个y轴的大小(value)*/
    float x_VLast;
    float y_VLase ;
    DataSourceType dataSourceType;//判断数据源的类型
    NSMutableArray *positionXArr;
    NSMutableArray *positionYArr;
    NSArray *defauleX;
    NSArray *defauleY;

}

@end

@implementation ZXYXYAxisView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        allArray = [[NSMutableArray alloc] init];
        maxOrMinValues = [[NSMutableDictionary alloc] init];
        self.backgroundColor = [UIColor clearColor];
        x_0 = 0.0f;
        y_0 = 0.0f;
        positionXArr = [[NSMutableArray alloc] init];
        positionYArr = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    self.backgroundColor = [UIColor clearColor];
    NSNumber *maxNumberx;
    NSNumber *maxNumbery ;
    if(dataSourceType == DataNUM)
    {
        maxNumberx = [maxOrMinValues objectForKey:maxX];
        maxNumbery = [maxOrMinValues objectForKey:maxY];
        if(maxNumberx.floatValue<100 && maxNumbery.floatValue<100)
        {
            [self drawDefaultAxis];
        }
        else
        {
            [self drawHigherAxis];
        }
    }
    else
    {
        [self drawHigherAxis];
    }
    
}

- (void)drawLineOutLine
{
    
}

// !!!:画图啦
- (void)putViewsXAxis:(NSArray *)defaultArrAxisX andYAxis:(NSArray *)defaultArrAxisY
{
    float xVO = 0;
    float yVO = 0;
    if(defaultArrAxisX.count >0)
    {
        xVO = [[defaultArrAxisX objectAtIndex:0] floatValue];
        if(defaultArrAxisY.count >2)
        {
            yVO = [[defaultArrAxisY objectAtIndex:1] floatValue];
        }
    }
    x_V0 = xVO;
    y_V0 = yVO;
    [self drawXAxis:defaultArrAxisX];
    [self drawYAxis:defaultArrAxisY];
    [self drawViewOnThis];
}

// !!!:绘制x坐标
-(void)drawXAxis:(NSArray *)defaultArrAxisX
{
    float lastX  ;
    if(dataSourceType == DataNUM||dataSourceType == DataNUMString)
    {
        lastX   = [defaultArrAxisX.lastObject floatValue];
        x_VLast = lastX;
    }
    for(int i = 0;i<defaultArrAxisX.count;i++)
    {
        UILabel *label;
        
        if(dataSourceType == DataNUM||dataSourceType == DataNUMString)
        {
            label = [[UILabel alloc] initWithFrame:CGRectMake(30+((self.frame.size.width-30)/(defaultArrAxisX.count+1))*(i+1), self.frame.size.height-20, 30, 20)];
            label.font = [UIFont systemFontOfSize:14];
            [label setTextAlignment:NSTextAlignmentCenter];
            [self addSubview:label];
            label.text = [NSString stringWithFormat:@"%@",[defaultArrAxisX objectAtIndex:i]];
        }
        else
        {
            label = [[UILabel alloc] initWithFrame:CGRectMake(60+((self.frame.size.width-30)/(defaultArrAxisX.count+1))*(i), self.frame.size.height-20, (self.frame.size.width-30)/(defaultArrAxisX.count+1), 20)];
            label.font = [UIFont systemFontOfSize:14];
            [label setLineBreakMode:NSLineBreakByWordWrapping];
            [label setTextAlignment:NSTextAlignmentCenter];
            label.text = [defaultArrAxisX objectAtIndex:i];
            [self addSubview:label];
            
        }
        float toEnd = self.frame.size.height- self.frame.size.height/defauleY.count*(defauleY.count-1)-self.frame.size.height/defauleY.count/2;
        [self drawLineBegain:CGPointMake(label.center.x, label.center.y-15) andEnd:CGPointMake(label.center.x, toEnd) isSpash:NO];
        [positionXArr addObject:[NSNumber numberWithFloat:label.center.x]];
        if(i == 0)
        {
            x_0 = label.center.x;
        }
        if(i == defaultArrAxisX.count-1)
        {
            x_last = label.center.x;
        }
    }

}

// !!!:绘制Y坐标
- (void)drawYAxis:(NSArray *)defaultArrAxisY
{
    float lastY  ;
    NSMutableArray *allLabelPosition = [[NSMutableArray alloc] init];
    if(dataSourceType == DataNUM || dataSourceType == DataStringNUM)
    {
        lastY   = [defaultArrAxisY.lastObject floatValue];
        y_VLase = lastY;
    }
    float delta = self.frame.size.height/defaultArrAxisY.count;
    for(int i = 1;i<=defaultArrAxisY.count;i++)
    {
        
        UILabel *label;
        if(dataSourceType == DataNUM || dataSourceType == DataStringNUM)
        {
            label = [[UILabel alloc] initWithFrame:CGRectMake(30, self.frame.size.height- self.frame.size.height/defaultArrAxisY.count*i, 30, self.frame.size.height/defaultArrAxisY.count)];
            [label setTextAlignment:NSTextAlignmentRight];

            label.text = [NSString stringWithFormat:@"%@",[defaultArrAxisY objectAtIndex:i-1]];
        }
        else
        {
            label = [[UILabel alloc] initWithFrame:CGRectMake(30, self.frame.size.height- self.frame.size.height/defaultArrAxisY.count*(i), 30, self.frame.size.height/defaultArrAxisY.count)];
            [label setTextAlignment:NSTextAlignmentRight];
            
            label.text = [NSString stringWithFormat:@"%@",[defaultArrAxisY objectAtIndex:i-1]];
            [positionYArr addObject:[NSNumber numberWithFloat:label.center.y]];
        }
        [self addSubview:label];
        if(i !=1 )
        {
            [self drawLineBegain:CGPointMake(label.center.x+20, label.center.y) andEnd:CGPointMake(self.frame.size.width, label.center.y) isSpash:NO];
        }
        label.font = [UIFont systemFontOfSize:14];
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:label.center.x],@"x",[NSNumber numberWithFloat:label.center.y],@"y", nil];
        [allLabelPosition addObject:dic];
        if(i == 2)
        {
            y_0 = label.center.y;
        }
        if(i == defaultArrAxisY.count)
        {
            y_last = label.center.y;
            [self drawBlueLineFromPoint:CGPointMake(label.center.x+20, label.center.y) andEndPoint:CGPointMake(self.frame.size.width, label.center.y)];
        }
        if(i == 1)
        {
            float toEnd = self.frame.size.height- self.frame.size.height/defauleY.count*(defauleY.count-1)-self.frame.size.height/defauleY.count/2;
            [self drawBlueLineFromPoint:CGPointMake(label.center.x+20, label.center.y) andEndPoint:CGPointMake(label.center.x+20, toEnd)];
            [self drawBlueLineFromPoint:CGPointMake(label.center.x+20, label.center.y) andEndPoint:CGPointMake(self.frame.size.width, label.center.y)];
            [self drawBlueLineFromPoint:CGPointMake(self.frame.size.width-1, label.center.y) andEndPoint:CGPointMake(self.frame.size.width-1, toEnd)];
        }
    }
    for(int i = 1;i<=defaultArrAxisY.count;i++)
    {
        if(i !=1 )
        {
            for(int j = 1;j<=4;j++)
            {
                NSDictionary *dic = [allLabelPosition objectAtIndex:i-1];
                [self drawLineBegain:CGPointMake([[dic objectForKey:@"x"] floatValue]+20, [[dic objectForKey:@"y"] floatValue]+delta/5*j) andEnd:CGPointMake(self.frame.size.width, [[dic objectForKey:@"y"] floatValue]+delta/5*j) isSpash:YES];
            }
        }

    }

}

- (void)drawBlueLineFromPoint:(CGPoint)begian andEndPoint:(CGPoint)end
{
    CGContextRef ctf ;
    ctf = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(ctf, 2);
    CGContextMoveToPoint(ctf, begian.x, begian.y);
    CGContextAddLineToPoint(ctf, end.x, end.y);
    CGContextSetStrokeColorWithColor(ctf, [UIColor colorWithRed:0.6353 green:0.7725 blue:0.8549 alpha:1].CGColor);
    CGContextStrokePath(ctf);
}

// !!!:画图
- (void)drawViewOnThis
{
    if([_dataSource respondsToSelector:@selector(axisView:viewAtData:)])
    {
        for(int i = 0;i<numOfData;i++)
        {
            UIView *views = [_dataSource axisView:self viewAtData:i];
            NSDictionary *location ;
            if(dataSourceType == DataNUM)
            location = [_dataSource axisView:self dataForAxisAtIndex:i];
            float x=0 ;
            float y=0;
            if(dataSourceType == DataNUM)
            {
                location = [_dataSource axisView:self dataForAxisAtIndex:i];
                x = [self scaleOfCurrentWithValue:[[location objectForKey:@"x"] floatValue] isXOrY:x_Type];
                y = [self scaleOfCurrentWithValue:[[location objectForKey:@"y"] floatValue] isXOrY:y_Type];
            }
            
            if(dataSourceType == DataNUMString)
            {
                location = [_dataSource axisView:self dataForAxisAtIndexYString:i];
                x = [self scaleOfCurrentWithValue:[[location objectForKey:@"x"] floatValue] isXOrY:x_Type];
            }
            if(dataSourceType == DataStringNUM)
            {
                location = [_dataSource axisView:self dataForAxisAtIndexXString:i];
                y = [self scaleOfCurrentWithValue:[[location objectForKey:@"y"] floatValue] isXOrY:y_Type];
                x = [[positionXArr objectAtIndex:i] floatValue];
            }
            else if (dataSourceType == DataNUMString)
            {
                location = [_dataSource axisView:self dataForAxisAtIndexYString:i];
                x = [self scaleOfCurrentWithValue:[[location objectForKey:@"x"] floatValue] isXOrY:x_Type];
                y = [[positionYArr objectAtIndex:i] floatValue];
            }
            else if (dataSourceType == DataSringString)
            {
                x = [[positionXArr objectAtIndex:i] floatValue];
                y = [[positionYArr objectAtIndex:i] floatValue];
            }
            
            float width = -[self scaleOfCurrentWithValue:[[views valueForKey:@"radius"] floatValue] isXOrY:width_Type];
            views.frame = CGRectMake(views.frame.origin.x, views.frame.origin.y, width, width);
            views.tag = 110+i;
            views.alpha = 0.1;
            [self addSubview:views];
            views.center = CGPointMake(x, y);
            [UIView animateWithDuration:0.3 animations:^{
                views.alpha = 0.8;
            }];
            if([_delegate respondsToSelector:@selector(axisView:didSelectAtIndex:)])
            {
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectItem:)];
                [views addGestureRecognizer:tap];
            }
            
        }
        
    }

}


- (void)selectItem:(UIGestureRecognizer *)ges
{

   NSInteger tags = ges.view.tag-110;
   NSLog(@"yes %ld",(long)ges.view.tag);
   [_delegate axisView:self didSelectAtIndex:tags];
}

// !!!:设施数据源
- (void)setDataSource:(id<ZXYAxisDatasource>)dataSource
{
    _dataSource = dataSource;
    if(allArray == nil)
    {
        allArray = [[NSMutableArray alloc] init];
        maxOrMinValues = [[NSMutableDictionary alloc] init];
    }
    if([dataSource respondsToSelector:@selector(axisViewNumberOfDataNum:)])
    {
        numOfData = [dataSource axisViewNumberOfDataNum:self];
    }
    else
    {
        NSAssert([dataSource respondsToSelector:@selector(axisViewNumberOfDataNum:)], @"ZXYAxisDatasource 没有实现代理方法axisViewNumberOfDataNum");
    }
    dataSourceType = [self getCurrentType];
    if(dataSourceType == DataNone)
    {
        NSAssert(dataSourceType!=DataNone, @"没有实现任何返回数据的代理axisView:dataForAxisAtIndex:");
        return;
    }
    allArray = [NSMutableArray arrayWithArray:[self dataOfType:dataSourceType]];
    maxOrMinValues = [NSMutableDictionary dictionaryWithDictionary:[self maxOrMinXYValues] ];
}

//根据类型选取所有的数据
- (NSMutableArray *)dataOfType:(DataSourceType)dataType
{
    NSMutableArray *tempDataArr = [[NSMutableArray alloc] init];
    for(int i = 0;i<numOfData;i++)
    {
        
        switch (dataType) {
            case DataNUM:
            {
                [tempDataArr addObject:[_dataSource axisView:self dataForAxisAtIndex:i]];
            }
                break;
            case DataStringNUM:
            {
                [tempDataArr addObject:[_dataSource axisView:self dataForAxisAtIndexXString:i]];
            }
                break;
            case DataNUMString:
            {
                [tempDataArr addObject:[_dataSource axisView:self dataForAxisAtIndexYString:i]];
            }
                break;
            case DataSringString:
            {
                [tempDataArr addObject:[_dataSource axisView:self dataForAxisAtIndexXYString:i]];
            }
                break;
            default:
                break;
        }
    }
    return tempDataArr;
}

- (DataSourceType)getCurrentType
{
    if([_dataSource respondsToSelector:@selector(axisView:dataForAxisAtIndex:)])
    {
        return DataNUM;
    }
    else if([_dataSource respondsToSelector:@selector(axisView:dataForAxisAtIndexXString:)])
    {
        return DataStringNUM;
    }
    else if([_dataSource respondsToSelector:@selector(axisView:dataForAxisAtIndexYString:)])
    {
        return DataNUMString;
    }
    else if([_dataSource respondsToSelector:@selector(axisView:dataForAxisAtIndexXYString:)])
    {
        return DataSringString;
    }
    else
    {
        NSAssert([_dataSource respondsToSelector:@selector(axisView:dataForAxisAtIndex:)], @"ZXYAxisDatasource 没有实现代理方法axisView:dataForAxisAtIndex:");
        return DataNone;
    }

}

- (void)setDelegate:(id<ZXYAxisDelegate>)delegate
{
    _delegate = delegate;
}

//默认坐标系
- (void)drawDefaultAxis
{
    defauleX = [NSArray arrayWithObjects:@0,@20,@40,@60,@80,@100 ,nil];
     defauleY = [NSArray arrayWithObjects:@-20,@0,@20,@40,@60,@80,@100,@120 ,nil];
    [self putViewsXAxis:defauleX andYAxis:defauleY];
    [self putViewToAxisView];
}

// !!!:坐标生成应该是y轴的第二个元素要与x轴的第一个元素相同
- (void)putViewToAxisView
{
    [self produceAxisDataMax:0 andMin:0];
}

// !!!:产生一组stirng类型的坐标
- (NSArray *)produceAxisString:(NSArray *)dataArry isXYType:(XYType)xyType
{
    NSMutableArray *array = [[NSMutableArray alloc] init];
    
    for(int i = 0;i<dataArry.count;i++)
    {
        NSDictionary *dic = [dataArry objectAtIndex:i];
        if(xyType == x_Type)
        {
            NSString *xValue = [dic objectForKey:@"x"];
            [array addObject:xValue];
        }
        else
        {
            NSString *yValue = [dic objectForKey:@"y"];
            [array addObject:yValue];
        }
    }
    
    return array;
}

// !!!:根据最大值与最小值之间的间距产生一组坐标
- (NSArray *)produceAxisDataMax:(float)maxValue andMin:(float)minValue
{
    DigitsType type = [self ANumberSNum:maxValue];
    NSLog(@"type is %d",type);
    
    if(maxValue - minValue<20)
    {
        NSArray *returnArr = [NSArray arrayWithObjects:[NSNumber numberWithFloat:minValue-10],[NSNumber numberWithFloat:minValue],[NSNumber numberWithFloat:minValue+10],[NSNumber numberWithFloat:minValue+20], nil];
        return returnArr;
    }
    else if(maxValue-minValue>=20&&maxValue-minValue<50)
    {
        NSArray *returnArr = [NSArray arrayWithObjects:[NSNumber numberWithFloat:minValue-10],[NSNumber numberWithFloat:minValue],[NSNumber numberWithFloat:minValue+10],[NSNumber numberWithFloat:minValue+20],[NSNumber numberWithFloat:minValue+30],[NSNumber numberWithFloat:minValue+40],[NSNumber numberWithFloat:minValue+50], nil];
        return returnArr;
    }
    else if(maxValue-minValue<80&&maxValue-minValue>=50)
    {
        NSArray *returnArr = [NSArray arrayWithObjects:[NSNumber numberWithFloat:minValue-10],[NSNumber numberWithFloat:minValue],[NSNumber numberWithFloat:minValue+10],[NSNumber numberWithFloat:minValue+20],[NSNumber numberWithFloat:minValue+30],[NSNumber numberWithFloat:minValue+40],[NSNumber numberWithFloat:minValue+50],[NSNumber numberWithFloat:minValue+60],[NSNumber numberWithFloat:minValue+70],[NSNumber numberWithFloat:minValue+80], nil];
        return returnArr;
    }
    else if(maxValue-minValue>=80 && maxValue-minValue<100)
    {
        NSArray *returnArr = [NSArray arrayWithObjects:[NSNumber numberWithFloat:minValue-20],[NSNumber numberWithFloat:minValue],[NSNumber numberWithFloat:minValue+20],[NSNumber numberWithFloat:minValue+40],[NSNumber numberWithFloat:minValue+60],[NSNumber numberWithFloat:minValue+80],[NSNumber numberWithFloat:minValue+100], nil];
        return returnArr;
    }
    else if(maxValue - minValue >=100 && maxValue-minValue<200)
    {
        NSArray *returnArr = [NSArray arrayWithObjects:[NSNumber numberWithFloat:minValue-40],[NSNumber numberWithFloat:minValue],[NSNumber numberWithFloat:minValue+40],[NSNumber numberWithFloat:minValue+80],[NSNumber numberWithFloat:minValue+120],[NSNumber numberWithFloat:minValue+160],[NSNumber numberWithFloat:minValue+200], nil];
        return returnArr;
    }
    else if(maxValue - minValue >=200 && maxValue-minValue<300)
    {
        NSArray *returnArr = [NSArray arrayWithObjects:[NSNumber numberWithFloat:minValue-60],[NSNumber numberWithFloat:minValue],[NSNumber numberWithFloat:minValue+60],[NSNumber numberWithFloat:minValue+120],[NSNumber numberWithFloat:minValue+180],[NSNumber numberWithFloat:minValue+240],[NSNumber numberWithFloat:minValue+300], nil];
        return returnArr;
    }
    else if(maxValue - minValue >=300 && maxValue-minValue<400)
    {
        NSArray *returnArr = [NSArray arrayWithObjects:[NSNumber numberWithFloat:minValue-80],[NSNumber numberWithFloat:minValue],[NSNumber numberWithFloat:minValue+80],[NSNumber numberWithFloat:minValue+160],[NSNumber numberWithFloat:minValue+240],[NSNumber numberWithFloat:minValue+320],[NSNumber numberWithFloat:minValue+400], nil];
        return returnArr;
    }
    else
    {
         NSArray *returnArr = [NSArray arrayWithObjects:[NSNumber numberWithFloat:minValue-80],[NSNumber numberWithFloat:minValue],[NSNumber numberWithFloat:minValue+80],[NSNumber numberWithFloat:minValue+160],[NSNumber numberWithFloat:minValue+240],[NSNumber numberWithFloat:minValue+320],[NSNumber numberWithFloat:minValue+400], nil];
        return returnArr;
    }
}

// !!!:得出是几位数
- (DigitsType)ANumberSNum:(float)inputNum
{
    NSInteger tenDivide = inputNum/10;
    if(tenDivide==0)
    {
        return DigitsTypeOne;
    }
    else if((tenDivide/=10) == 0)
    {
        return DigitsTypeTen;
    }
    else if ((tenDivide/=10) == 0)
    {
        return DigitsTypeHund;
    }
    else if ((tenDivide/=10)==0)
    {
        return DigitsTypeThoud;
    }
    else return DigitsTypeTenThoud;
}


//根据传来的值 确定所在位置
-(float)scaleOfCurrentWithValue:(float)value isXOrY:(XYType)isXY
{
    float point = 0.0f;
    float scale = 0.0f;
    if(isXY == x_Type)
    {
        scale = (x_last - x_0)/(x_VLast-x_V0);
        point = (value-x_V0)*scale+x_0;
    }
    else if(isXY == y_Type)
    {
        scale = (y_last - y_0)/(y_VLase - y_V0);
        point = (value-y_V0)*scale+y_0;
    }
    else
    {
        if(dataSourceType == DataNUMString)
        {
            scale = (x_last - x_0)/(x_VLast-x_V0);
        }
        else if(dataSourceType == DataStringNUM)
        {
            scale = (y_last - y_0)/(y_VLase - y_V0);
        }
        else if(dataSourceType == DataNUM)
        {
            scale = (x_last - x_0)/(x_VLast-x_V0);
        }
        else
        {
            scale = 1;
        }
        point = value*scale;
    }
    ;
    return point;
    
}

//开始作图
- (void)drawHigherAxis
{
    float maxXValue ;
    float minXValue ;
    float maxYValue ;
    float minYValue ;
    if(dataSourceType == DataNUM)
    {
        maxXValue = [[maxOrMinValues objectForKey:maxX] floatValue];
        minXValue = [[maxOrMinValues objectForKey:minX] floatValue];
        maxYValue = [[maxOrMinValues objectForKey:maxY] floatValue];
        minYValue = [[maxOrMinValues objectForKey:minY] floatValue];
        defauleX = [NSArray arrayWithArray:[self produceAxisDataMax:maxXValue andMin:minXValue] ];
        defauleY = [NSArray arrayWithArray:[self produceAxisDataMax:maxYValue andMin:minYValue] ];
    }
    else if(dataSourceType == DataStringNUM)
    {
        maxYValue = [[maxOrMinValues objectForKey:maxY] floatValue];
        minYValue = [[maxOrMinValues objectForKey:minY] floatValue];
        defauleY = [NSArray arrayWithArray:[self produceAxisDataMax:maxYValue andMin:minYValue] ];
        defauleX = [NSArray arrayWithArray:[self produceAxisString:allArray isXYType:x_Type]];
    }
    else if(dataSourceType == DataNUMString)
    {
        maxXValue = [[maxOrMinValues objectForKey:maxX] floatValue];
        minXValue = [[maxOrMinValues objectForKey:minX] floatValue];
        defauleY = [NSArray arrayWithArray:[self produceAxisString:allArray isXYType:y_Type] ];
        defauleX = [NSArray arrayWithArray:[self produceAxisDataMax:maxXValue andMin:minXValue]];
    }
    else
    {
        defauleY = [NSArray arrayWithArray:[self produceAxisString:allArray isXYType:y_Type] ];
        defauleX = [NSArray arrayWithArray:[self produceAxisString:allArray isXYType:x_Type]];
    }
    
    
    [self putViewsXAxis:defauleX andYAxis:defauleY];
}

- (void)drawLineBegain:(CGPoint)begian andEnd:(CGPoint)end isSpash:(BOOL)isSpash
{
    CGContextRef ctf ;
    ctf = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(ctf, 1);
    CGContextMoveToPoint(ctf, begian.x, begian.y);
    CGContextAddLineToPoint(ctf, end.x, end.y);
    CGContextSetStrokeColorWithColor(ctf, [UIColor grayColor].CGColor);
    if(!isSpash)
    {
        
        CGContextSetAlpha(ctf, 0.8);
    }
    else
    {
        CGFloat lengths[] = {1,1};
        CGContextSetLineDash(ctf, 1, lengths,1);
        CGContextSetAlpha(ctf, 0.3);
    }

    
    
    CGContextStrokePath(ctf);
    CGContextFlush(ctf);
    
}

//挑选出最大值与最小值；
- (NSDictionary *)maxOrMinXYValues
{
    float maxxValue = 0;
    float minxValue = 0;
    float maxyValue = 0;
    float minyValue = 0;
    NSNumber *xNMx  = [NSNumber numberWithInt:0];
    NSNumber *xNMn  = [NSNumber numberWithInt:0];
    NSNumber *yNMx  = [NSNumber numberWithInt:0];
    NSNumber *ynMn  = [NSNumber numberWithInt:0];
    if(numOfData == 0)
    {
        
    }
    else
    {
        NSDictionary *currentDics = [allArray objectAtIndex:0];
        NSNumber *xs;
        NSNumber *ys ;
        if(dataSourceType == DataNUM || DataNUMString)
        {
            xs = [currentDics objectForKey:@"x"];
            minxValue = xs.floatValue;
        }
        if(dataSourceType == DataNUM || dataSourceType == DataStringNUM)
        {
            ys = [currentDics objectForKey:@"y"];
            minyValue = ys.floatValue;
        }
        for(int i = 0;i<allArray.count;i++)
        {
            NSDictionary *currentDic = [allArray objectAtIndex:i];
            NSNumber *x;
            NSNumber *y;
            x = [currentDic objectForKey:@"x"];
            y = [currentDic objectForKey:@"y"];
            if(dataSourceType == DataNUMString || dataSourceType == DataNUM)
            {
                x = [currentDic objectForKey:@"x"];
                if(maxxValue<x.floatValue)
                {
                    maxxValue = x.floatValue;
                }
                if(minxValue>x.floatValue)
                {
                    minxValue = x.floatValue;
                }
            }
            if(dataSourceType == DataNUM || dataSourceType == DataStringNUM)
            {
                y = [currentDic objectForKey:@"y"];
                if(maxyValue<y.floatValue)
                {
                    maxyValue = y.floatValue;
                }
                if(minyValue>y.floatValue)
                {
                    minyValue = y.floatValue;
                }
            }
        }
        if(dataSourceType == DataNUMString || dataSourceType == DataNUM)
        {
            xNMx = [NSNumber numberWithFloat:maxxValue];
            xNMn = [NSNumber numberWithFloat:minxValue];
        }
        if(dataSourceType == DataNUM || dataSourceType == DataStringNUM)
        {
            yNMx = [NSNumber numberWithFloat:maxyValue];
            ynMn = [NSNumber numberWithFloat:minyValue];
        }
    }
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:xNMx,maxX,xNMn,minX,yNMx,maxY,ynMn,minY, nil];
    return dic;
}

- (void)reloadAxisData
{
    for(UIView *subViewss in self.subviews)
    {
        [subViewss removeFromSuperview];
    }
    [positionXArr removeAllObjects];
    [positionYArr removeAllObjects];
    [allArray removeAllObjects];
    self.delegate = _delegate;
    self.dataSource = _dataSource;
    [self setNeedsDisplay];
}

@end
